//
//  DetailViewController.m
//  NewChat
//
//  Created by songKX on 16/3/3.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "DetailViewController.h"

#import "RootViewController.h"

#import "AppDelegate.h"

#import "EnterViewController.h"

#import <AVOSCloud/AVOSCloud.h>

#import <EaseMob.h>

@interface DetailViewController ()
@property (weak, nonatomic) IBOutlet UIButton *manBtn;
@property (weak, nonatomic) IBOutlet UIButton *womanBtn;

@property (weak, nonatomic) IBOutlet UITextField *nickName;


@property (weak, nonatomic) IBOutlet UIButton *userDate;

@property (nonatomic , strong)NSString *userGender;

@end

@implementation DetailViewController

-(instancetype)init{
    
    self = [super init];
    
    if (self) {
        
        UIStoryboard *temp = [UIStoryboard storyboardWithName:@"Login" bundle:nil];
        
        self = [temp instantiateViewControllerWithIdentifier:@"detailVC"];
        
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.manBtn.layer.borderColor = [UIColor redColor].CGColor;
    
    self.manBtn.layer.borderWidth = 1;
    
    self.womanBtn.layer.borderColor = [UIColor cyanColor].CGColor;
    
    self.womanBtn.layer.borderWidth = 1;
    
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(saveUserInfo)];
    
    
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

//选择性别

- (IBAction)chooseGender:(UIButton *)sender {
    
    self.manBtn.backgroundColor = [UIColor grayColor];
    
    self.womanBtn.backgroundColor = [UIColor grayColor];
    
    [self.womanBtn setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];
    
     [self.manBtn setTitleColor:[UIColor orangeColor] forState:(UIControlStateNormal)];

    
    [sender setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    
    sender.backgroundColor = [UIColor blueColor];
    
    self.userGender = sender.titleLabel.text;
    
}


//保存用户信息

-(void)saveUserInfo{
    
    NSLog(@"保存用户信息");
    
    //环信注册,成功跳转登录
    
    [[EaseMob sharedInstance].chatManager asyncLoginWithUsername:self.userName password:self.userPasswd completion:^(NSDictionary *loginInfo, EMError *error) {
        
        if(!error&&loginInfo){
            
            EnterViewController *temp = [[UIStoryboard storyboardWithName:@"Login" bundle:nil] instantiateViewControllerWithIdentifier:@"enter"];
            
            temp.userNameText = self.userName;
            
            temp.userPwdText = self.userPasswd;
            
            //leancloud注册
            
            //新建用户
            AVUser *user = [AVUser user];
            //用户名
            user.username = self.userName;
            //密码
            user.password = self.userPasswd;
            //昵称
            [user setObject:self.nickName.text forKey:@"nickname"];
            //性别
            [user setObject:self.userGender forKey:@"gender"];
            //生日
            [user setObject:self.userDate.titleLabel.text forKey:@"birthday"];
            //签名
            [user setObject:nil forKey:@"signature"];
            
            [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
                if (succeeded) {
                    // 注册成功
                    
                    [self.navigationController pushViewController:temp animated:YES];
                    
                } else {
                    NSLog(@"注册失败");
                }
            }];
            
        }
        
    } onQueue:dispatch_get_main_queue()];
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
